package com.qingdai.controller;

import com.github.yulichang.query.MPJQueryWrapper;
import com.qingdai.emuns.Address;
import com.qingdai.emuns.AddressDTO;
import com.qingdai.emuns.Commodity;
import com.qingdai.emuns.Shop;
import com.qingdai.entities.CommonResult;
import com.qingdai.service.AddressService;
import com.qingdai.service.CommodityEsService;
import com.qingdai.service.CommodityService;
import com.qingdai.service.ShopService;
import com.qingdai.utils.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author LiuZiMing
 * @since 2024-01-26
 */
@RestController
@RequestMapping("/commodity")
@Tag(name = "商品")
public class CommodityController {
    @Resource
    private CommodityService commodityService;
    @Resource
    private CommodityEsService commodityEsService;
    @Resource
    private ShopService shopService;
    @Resource
    private AddressService addressService;
    @Resource
    private JwtUtil jwtUtil;

    @PostMapping("/es")
    @Operation(summary = "所有商品存到ES", description = "")
    @Parameters({
//            @Parameter(name = "id", description = "id", required = true),
    })
    private CommonResult<List<Commodity>> addAddressesToES() {
        List<Commodity> allCommodity = commodityService.list();
        Iterable<Commodity> commoditiesIterable = commodityEsService.saveCommodities(allCommodity);
        return new CommonResult<>(200, "所有商品存到ES成功");
    }

    //根据id,jwt获取commodities
    @GetMapping("/jwt/shopId")
    @Operation(summary = "根据shopId获取商品", description = "根据shopId获取商品")
    @Parameters({
            @Parameter(name = "shopId", description = "店铺id", required = true),
    })
    private CommonResult<List<Address>> getCommoditiesByShopIdAndJwt(Integer shopId,
                                                                     HttpServletRequest request)
    {
        CommonResult userIdByJwtCommonResult = jwtUtil.getUserIdByJwt(request);
            Integer jwtUserId = (Integer) userIdByJwtCommonResult.getData();
            MPJQueryWrapper mpjQueryWrapper = new MPJQueryWrapper();
            mpjQueryWrapper.select("*").eq("shop_id", shopId);
            List<Address> list = commodityService.list(mpjQueryWrapper);
            MPJQueryWrapper mpjQueryWrapper2 = new MPJQueryWrapper();
            mpjQueryWrapper.select("*").eq("shop_id", shopId);
            Shop shop = shopService.getOne(mpjQueryWrapper2);
            if (shop == null) {
                return new CommonResult<>(400, "店铺id非法");
            } else if (shop.getSellerId() == jwtUserId) {
                return new CommonResult<>(200, "获取本店所有商品成功", list);
            } else {
                return new CommonResult<>(400, "非本人店铺");
            }
    }

    @PostMapping("/jwt/shopId")
    @Operation(summary = "根据shopId创建新商品", description = "根据shopId创建新商品")
    @Parameters({
//            @Parameter(name = "id", description = "id", required = true),
            @Parameter(name = "title", description = "标题", required = true),
            @Parameter(name = "price", description = "价格", required = true),
            @Parameter(name = "shippingAddressId", description = "发货地址id", required = true),
            @Parameter(name = "describe", description = "描述", required = true),
            @Parameter(name = "shopId", description = "商店id", required = true),
    })
    public CommonResult createCommoditiesByJwt(@RequestParam(required = true) String title,
                                               @RequestParam(required = true) Double price,
                                               @RequestParam(required = true) Integer shippingAddressId,
                                               @RequestParam(required = true) String wDescribe,
                                               @RequestParam(required = true) Integer shopId,
                                               HttpServletRequest request)
    {
        CommonResult result = jwtUtil.getUserIdByJwt(request);
            Integer jwtUserId = (Integer) result.getData();
            Shop shop = shopService.getById(shopId);
            Address address = addressService.getById(shippingAddressId);
            if (shop == null || address == null) {
                return new CommonResult<>(400, "店铺id或地址id非法");
            } else if (Objects.equals(shop.getSellerId(), jwtUserId) || Objects.equals(address.getUserId(), jwtUserId)) {
                boolean createResult = commodityService.save(new Commodity(null, title, price, shippingAddressId, wDescribe, shopId));
                if (createResult) {
                    return new CommonResult(200, "创建商品成功");
                } else {
                    return new CommonResult(400, "创建商品失败");
                }
            } else {
                return new CommonResult<>(400, "非本人店铺或地址");
            }
    }

    @PutMapping("/jwt/id")
    @Operation(summary = "根据id更改商品", description = "根据id更改商品")
    @Parameters({
            @Parameter(name = "id", description = "id", required = false),
            @Parameter(name = "title", description = "标题", required = false),
            @Parameter(name = "price", description = "价格", required = false),
            @Parameter(name = "shippingAddressId", description = "发货地址id", required = false),
            @Parameter(name = "describe", description = "描述", required = false),
            @Parameter(name = "shopId", description = "商店id", required = false),

    })
    public CommonResult updateCommoditiesByIdAndJwt(@RequestParam(required = false) Integer id,
                                                    @RequestParam(required = false) String title,
                                                    @RequestParam(required = false) Double price,
                                                    @RequestParam(required = false) Integer shippingAddressId,
                                                    @RequestParam(required = false) String wDescribe,
                                                    HttpServletRequest request)
    {
        CommonResult result = jwtUtil.getUserIdByJwt(request);
            Integer jwtUserId = (Integer) result.getData();
            Commodity commodity = commodityService.getById(id);
            if (commodity == null) {
                return new CommonResult<>(400, "id非法");
            } else {
                Integer shopId = commodity.getShopId();
                Address address = addressService.getById(shippingAddressId);
                if (address == null) {
                    return new CommonResult<>(400, "地址id非法");
                } else if (Objects.equals(address.getUserId(), jwtUserId)) {
                    boolean updateResult = commodityService.saveOrUpdate(new Commodity(id, title, price, shippingAddressId, wDescribe, shopId));
                    if (updateResult) {
                        return new CommonResult(200, "修改商品成功");
                    } else {
                        return new CommonResult(400, "修改商品失败");
                    }
                } else {
                    return new CommonResult<>(400, "非本人地址");
                }
            }
    }

    @DeleteMapping("/jwt/id")
    @Operation(summary = "根据id删除商品", description = "根据id删除商品")
    @Parameters({@Parameter(name = "id", description = "id", required = true),})
    public CommonResult deleteCommoditiesByIdAndJwt(@RequestParam(required = true) Integer id,
                                                    HttpServletRequest request)
    {
        CommonResult result = jwtUtil.getUserIdByJwt(request);
            Integer jwtUserId = (Integer) result.getData();
            Commodity commodity = commodityService.getById(id);
            if (commodity == null) {
                return new CommonResult<>(400, "id非法");
            }
            Integer shopId = commodity.getShopId();
            Shop shop = shopService.getById(shopId);
            if (Objects.equals(shop.getSellerId(), jwtUserId)) {
                boolean deleteResult = commodityService.removeById(id);
                if (deleteResult) {
                    return new CommonResult(200, "删除商品成功");
                } else {
                    return new CommonResult(400, "删除商品失败");
                }
            } else {
                return new CommonResult<>(400, "非本人店铺商品");
            }
    }
}
